#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a);i <= (b);i++)
#define ep emplace_back
#define pb push_back
#define PII pair<int,int>
#define fi first
#define se second
char buf[1 << 20],*p1,*p2;
#define gc() ((p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1 << 20,stdin),p1 == p2)) ? 0 : *p1++)
int read(){
    int x = 0,f = 1,ch = gc();
    while(!isdigit(ch)){
        if(ch == '-') f = -1;
        ch = gc();
    }
    while(isdigit(ch)){
        x = x * 10 + (ch ^ 48);
        ch = gc();
    }
    return x * f;
}
const int N = 200005;
struct node{
    int l,r,ls,rs;
    int val,laz;
}seg[N << 1];
int cnt;
int ls(int node){return seg[node].ls;}
int rs(int node){return seg[node].rs;}
void pushup(int node){
    seg[node].val = max(seg[ls(node)].val,seg[rs(node)].val);
}
void pushdown(int node){
    seg[node].val += seg[node].laz;
    if(ls(node)) seg[ls(node)].laz += seg[node].laz;
    if(rs(node)) seg[rs(node)].laz += seg[node].laz;
    seg[node].laz = 0;
}
void build(int l,int r){
    int node = ++cnt;
    seg[node].l = l,seg[node].r = r;
    seg[node].ls = seg[node].rs = 0;
    seg[node].laz = seg[node].val = 0;
    if(l == r){
        return;
    }
    int mid = (l + r) >> 1;
    seg[node].ls = cnt + 1;
    build(l,mid);
    seg[node].rs = cnt + 1;
    build(mid + 1,r);
    pushup(node);
}
int query(int L,int R,int node = 1){
    pushdown(node);
    int l = seg[node].l,r = seg[node].r;
    if(L <= l && r <= R) return seg[node].val;
    if(r < L || l > R) return 0;
    return max(query(L,R,ls(node)),query(L,R,rs(node)));
}
void update(int L,int R,int x,int node = 1){
    pushdown(node);
    int l = seg[node].l,r = seg[node].r;
    if(L <= l && r <= R){
        seg[node].laz += x;
        pushdown(node);
        return;
    }
    if(r < L || l > R) return;
    update(L,R,x,ls(node));
    update(L,R,x,rs(node));
    pushup(node);
}
int n,a[N];
int ton[1000005],las[N];
int T;
void solve(){
    memset(ton,0,sizeof(ton));
    memset(las,0,sizeof(las));
    cnt = 0;
    n = read();
    build(1,n);
    rep(i,1,n){
        a[i] = read();
        if(ton[a[i]]){
            las[i] = ton[a[i]];
        }
        ton[a[i]] = i;
    }
    rep(i,1,n){
        int dp = 0;
        if(las[i]){
            dp = query(las[i],las[i]) + a[i];
        }
        dp = max(dp,query(1,i - 1));
        update(i,i,dp);
        if(i >= 2 && a[i] == a[i - 1]) update(0,i - 2,a[i]);
        if(i >= 3 && a[i] == a[i - 2]) update(i - 1,i - 1,a[i]);
    }
    cout << query(1,n) << endl;
}
int main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    T = read();
    while(T--){
        solve();
    }
    return 0;
}
